package com.jz.community.moduleshopping.orderDetail.ui;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseHtml5Activity;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.base.BaseX5WebActivity;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.RxTextTool;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.R2;
import com.jz.community.moduleshopping.orderDetail.adapter.OrderTrackAdapter;
import com.jz.community.moduleshopping.orderDetail.bean.OrderTrackInfo;
import com.jz.community.moduleshopping.orderDetail.task.OrderTrackTask;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Target on 2018/4/6.
 * 订单跟踪信息
 */
public class OrderTrackActivity extends BaseMvpActivity {
    @BindView(R2.id.order_track_back)
    ImageView orderTrackBack;
    @BindView(R2.id.order_track_title)
    TextView orderTrackTitle;
    @BindView(R2.id.order_track_service)
    ImageView orderTrackService;
    @BindView(R2.id.order_track_toolbar)
    Toolbar orderTrackToolbar;
    @BindView(R2.id.order_track_express_iv)
    ImageView orderTrackExpressIv;
    @BindView(R2.id.order_track_express_co)
    TextView orderTrackExpressCo;//物流公司
    @BindView(R2.id.order_track_express_num)
    TextView orderTrackExpressNum;//物流单号
    @BindView(R2.id.order_track_express_copy)
    TextView orderTrackExpressCopy;//复制按钮
    @BindView(R2.id.order_track_express_rl)
    RelativeLayout orderTrackExpressRl;//快递Rl
    @BindView(R2.id.order_track_self_iv)
    ImageView orderTrackSelfIv;
    @BindView(R2.id.order_track_self_shop)
    TextView orderTrackSelfShop;//自提点名称
    @BindView(R2.id.order_track_self_address)
    TextView orderTrackSelfAddress;//自提点地址
    @BindView(R2.id.order_track_self_phone)
    TextView orderTrackSelfPhone;//自提点电话
    @BindView(R2.id.order_track_self_rl)
    RelativeLayout orderTrackSelfRl;//自提Rl
    @BindView(R2.id.order_track_rv)
    RecyclerView orderTrackRv;
    @BindView(R2.id.order_track_parent)
    LinearLayout orderTrackParent;

    private String orderId;
    private String address;
    private String sendType;
    private String phone;
    private String mentionPhone;
    private String expressName;
    private String expressCode;
    private OrderTrackAdapter adapter_track;

    @Override
    protected int getContentView() {
        return R.layout.module_shopping_activity_order_track;
    }

    @Override
    protected void initView() {
        setImmersionBar(orderTrackToolbar);
        orderTrackTitle.setText("订单跟踪");
    }

    @Override
    protected void initDatas() {
        Intent intent =  getIntent();
        orderId = intent.getStringExtra("orderId");
        address = intent.getStringExtra("address");
        sendType = intent.getStringExtra("sendType");
        phone = intent.getStringExtra("phone");
        mentionPhone = intent.getStringExtra("mentionPhone");
        expressName = intent.getStringExtra("expressName");
        expressCode = intent.getStringExtra("expressCode");
        handleOrderSendTypeShow();
        initOrderTrackData();
    }

    private void initOrderTrackData() {
        new OrderTrackTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                OrderTrackInfo orderTrackInfo = (OrderTrackInfo) o;
                if (!Preconditions.isNullOrEmpty(orderTrackInfo) && !Preconditions.isNullOrEmpty(orderTrackInfo.get_embedded()) && !Preconditions.isNullOrEmpty(orderTrackInfo.get_embedded().getContent())) {
                    handleBindOrderTrackAdapter(orderTrackInfo.get_embedded());
                }
            }
        }).execute(orderId);
    }

    private void handleBindOrderTrackAdapter(OrderTrackInfo.EmbeddedBean embeddedBean) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        orderTrackRv.setHasFixedSize(true);
        orderTrackRv.setFocusable(false);
        orderTrackRv.setLayoutManager(layoutManager);
        adapter_track = new OrderTrackAdapter(R.layout.module_shopping_item_order_track, embeddedBean.getContent());
        adapter_track.openLoadAnimation(BaseQuickAdapter.ALPHAIN);//加载动画
        orderTrackRv.setAdapter(adapter_track);
    }

    /**
     * 不同配送方式处理
     */
    private void handleOrderSendTypeShow() {

        if ("2".equals(sendType)) {
            SHelper.vis(orderTrackSelfRl);
            SHelper.gone(orderTrackExpressRl);
            //自提
            orderTrackSelfShop.setText("配送方式：自提");
            orderTrackSelfAddress.setText("自提点：" + address);

            if (!Preconditions.isNullOrEmpty(mentionPhone)) {
                SHelper.vis(orderTrackSelfPhone);
                handleOrderTelClick();
            } else {
                SHelper.gone(orderTrackSelfPhone);
            }

        } else if ("1".equals(sendType)) {
            SHelper.gone(orderTrackSelfRl);
            SHelper.vis(orderTrackExpressRl);
            //快递
            if (!Preconditions.isNullOrEmpty(expressName)) {
                orderTrackExpressCo.setText("物流公司：" + expressName);
            } else {
                orderTrackExpressCo.setText("物流公司：暂无");
            }

            if (!Preconditions.isNullOrEmpty(expressCode)) {
                orderTrackExpressNum.setText("运单号：" + expressCode);
                SHelper.vis(orderTrackExpressCopy);
            } else {
                orderTrackExpressNum.setText("运单号：暂无");
                SHelper.gone(orderTrackExpressCopy);
            }
        }
    }

    private void handleOrderTelClick() {
        // 响应点击事件的话必须设置以下属性
        orderTrackSelfPhone.setMovementMethod(LinkMovementMethod.getInstance());
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(getResources().getColor(R.color.home_red));
                ds.setUnderlineText(true);
            }
        };
        RxTextTool.getBuilder("")
                .append("联系电话: ")
                .append(phone)
                .setClickSpan(clickableSpan)
                .into(orderTrackSelfPhone);
    }

    //复制运单号
    private void copy(String copyContent) {
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("", copyContent);
        // 将ClipData内容放到系统剪贴板里。
        if (null != cm) {
            cm.setPrimaryClip(mClipData);
        }
    }

    @OnClick(R2.id.order_track_back)
    void orderTrackBackClick() {
        finish();
    }

    @OnClick(R2.id.order_track_service)
    void orderTrackServiceClick() {
        //客服
        startActivity(new Intent(OrderTrackActivity.this, BaseX5WebActivity.class)
                .putExtra("url", Constant.CUSTOM_SERVICE_URL)
                .putExtra("title", "在线客服"));
    }

    @OnClick(R2.id.order_track_express_copy)
    void orderTrackExpressCopyClick() {
        //复制
        String copyContent = orderTrackExpressNum.getText().toString().trim();
        if (!Preconditions.isNullOrEmpty(copyContent)) {
            copy(copyContent);
            WpToast.l(this, "已复制");
        }
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }
}
